<template>
  <div>
    <el-dialog
      :title="title"
      :closeOnClickModal="false"
      :visible.sync="show"
      width="650px">
      <el-form 
        label-position="right" 
        :model="selectTable"
        ref="addForm" 
        label-width="110px" 
        :inline="false"
        autocomplete="off"
        >
        <el-form-item label="任务名称" prop="beanName" :rules="[rule[0]]">
          <el-input placeholder="请输入" v-model="selectTable.beanName" maxLength="100"></el-input>
        </el-form-item>
        <el-row>
          <el-col :span="12">
        <el-form-item label="函数名" prop="methodName" :rules="[rule[0]]">
          <el-input placeholder="请输入" v-model="selectTable.methodName" maxLength="100"></el-input>
        </el-form-item>
          </el-col>
          <el-col :span="12">
        <el-form-item label="参数" prop="params">
          <el-input placeholder="请输入" v-model="selectTable.params" maxLength="100"></el-input>
        </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="Corn表达式" prop="cronExpression" :rules="[rule[0]]">
          <el-input placeholder="请输入" v-model="selectTable.cronExpression" maxLength="100"></el-input>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input placeholder="请输入" v-model="selectTable.remark" maxLength="100"></el-input>
        </el-form-item>
        <el-form-item label="状态" prop="status" :rules ="[rule[0]]">
          <el-switch
            v-model="selectTable.status"
            :active-value="0"
            :inactive-value="1"
            active-text="启用"
            inactive-text="停用"
            >
          </el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" v-loading="pload">
        <el-button @click="show = false">取 消</el-button>
        <el-button type="primary" @click="submitForm('addForm')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import * as api from '@/utils/api'
export default {
  components: {
  },
  props: {
    data: Object
  },
  created() {
    this.selectTable = this.JSONs(this.data)
  },
  watch: {
    show(newVal) {
      this.$parent.edit = false
    }
  },
  computed: {
    title() {
      if (this.selectTable.method === 'edit') {
        return '修改'
      } else if (this.selectTable.method === 'add') {
        return '添加'
      }
    }
  },
  data() {
    return {
      pload: false,
      rule: [
        { required: true, message: '必填项', trigger: 'change' }
      ],
      show: true,
      selectTable: {}
    }
  },
  methods: {
    async submitForm(formName) {
      await this.$refs[formName].validate()
      try {
        this.pload = true
        if (this.selectTable.method === 'edit') {
          await api.scheduleModify(this.selectTable)
        } else if (this.selectTable.method === 'add') {
          await api.scheduleSave(this.selectTable)
        }
        this.$notify({ title: '操作成功', type: 'success', duration: 5000 })
        this.$parent.edit = false
        this.$emit('getData')
      } catch (e) {
        this.pload = false
      }
    }
  }
}
</script>